package com.xmall.controller.admin;

import com.xmall.bean.vo.BaseRespVo;
import com.xmall.bean.table.GrouponRules;
import com.xmall.service.GrouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("admin/groupon")
public class GrouponController {

    @Autowired
    GrouponService grouponService;

    @RequestMapping("list")
    public BaseRespVo list(@RequestParam Map<String,String> params){
        Map<String, Object> result = grouponService.queryList(params);
        return BaseRespVo.ok(result);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody GrouponRules grouponRules){
        GrouponRules create = grouponService.createGrouponRule(grouponRules);

        //创建失败
        if (create == null){
            BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
            baseRespVo.setErrno(401);
            baseRespVo.setErrmsg("参数不对");
            return baseRespVo;
        }

        return BaseRespVo.ok(create);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody GrouponRules grouponRules){
        boolean success = grouponService.update(grouponRules);
        if (!success){
            return BaseRespVo.fail("参数值不对");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody GrouponRules grouponRules){
        grouponService.delete(grouponRules);
        return BaseRespVo.ok();
    }

    @RequestMapping("listRecord")
    public BaseRespVo listRecord(@RequestParam Map<String, String> params){
        Map<String, Object> result = grouponService.queryListRecord(params);
        return BaseRespVo.ok(result);
    }
}
